import json
import scrapy
# from bs4 import BeautifulSoup
from 爬虫.eastmoney.eastmoney.items import company_transaction_data_daily
from lxml import etree


class company_transaction_data_daily_spdier(scrapy.Spider):
    name = 'company_transaction_data_daily'
    allowed_domains = ['data.eastmoney.com']
    start_urls = ['https://quote.eastmoney.com/center/gridlist.html#newshares']
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
         'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }

    def parse(self, response, **kwargs):
        item = company_transaction_data_daily()
        tree = etree.HTML(response.text)
        # 获取到当前页面的数据
        tr_list = tree.xpath("//table[@id='table_wrapper-table']//tbody/tr")
        for tr in tr_list:
            item['today_open'] = tr.find_element_by_xpath("./td[13]/span").text  # 今开
            item['highest'] = tr.find_element_by_xpath("./td[11]/span").text  # 最高
            item['turnover'] = tr.find_element_by_xpath("./td[15]").text  # 换手
            item['trading_volume'] = tr.find_element_by_xpath("./td[8]").text  # 成交量
            item['market_value_added'] = tr.find_element_by_xpath("./td[16]").text  # 市营
            item['total_market_value'] = tr.find_element_by_xpath("./td[18]").text  # 总市值
            item['yesterday_close'] = tr.find_element_by_xpath("./td[14]").text  # 昨收
            item['lowest'] = tr.find_element_by_xpath("./td[12]/span").text  # 最低
            item['volume_ratio'] = tr.find_element_by_xpath("./td[18]").text  # 量比
            item['trading_amount'] = tr.find_element_by_xpath("./td[9]").text  # 成交额
            item['market_value_clean'] = tr.find_element_by_xpath("./td[17]").text  # 市净
            item['circulation_market_value'] = tr.find_element_by_xpath("./td[18]").text  # 流通市值
            # 传数据
            yield item
